package cn.lyvust.eais.api;


import cn.lyvust.eais.common.annotation.SysLogs;
import cn.lyvust.eais.common.bean.ResponseCode;
import cn.lyvust.eais.common.bean.ResponseResult;
import cn.lyvust.eais.common.exception.RequestException;
import cn.lyvust.eais.common.util.BeansUtil;
import cn.lyvust.eais.manage.dto.param.evaluation.ProjectItemScoreParam;
import cn.lyvust.eais.manage.entity.EvaluationProjectItemScore;
import cn.lyvust.eais.manage.service.EvaluationProjectItemScoreService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ggzheng
 * @since 2021-01-21
 */
@Api(tags = {"考核记录得分"})
@RestController
@RequestMapping("/evaluation/project_item_score")
public class EvaluationProjectItemScoreController {
    @Autowired
    private EvaluationProjectItemScoreService projectItemScoreService;

    @PostMapping(value = {"/list"})
    @ApiOperation(value = "查询考核记录得分")
    public ResponseResult list(@RequestBody ProjectItemScoreParam param){
        QueryWrapper wrapper = new QueryWrapper();
        if (!StringUtils.isEmpty(param.getId())) {
            wrapper.eq("id", param.getId());
        }
        if (!StringUtils.isEmpty(param.getUserId())) {
            wrapper.eq("user_id", param.getUserId());
        }
        if (!StringUtils.isEmpty(param.getItemId())) {
            wrapper.eq("item_id", param.getItemId());
        }
        if (!StringUtils.isEmpty(param.getCreateBy())) {
            wrapper.eq("create_by", param.getCreateBy());
        }
        wrapper.orderByDesc("update_time");
        return ResponseResult.e(ResponseCode.OK, projectItemScoreService.list(wrapper));
    }

    @PostMapping(value = {"/add"})
    @ApiOperation(value = "新增一个考核记录得分")
    @SysLogs("新增一个考核记录得分")
    public ResponseResult add(@RequestBody ProjectItemScoreParam param){
        if (StringUtils.isEmpty(param.getItemId())) {
            throw RequestException.fail("添加失败,考核记录的主键Id不能为空!");
        }
        if (StringUtils.isEmpty(param.getUserId())) {
            throw RequestException.fail("添加失败,考核记录得分添加操作人Id不能为空!");
        }
        if (StringUtils.isEmpty(param.getCreateBy())) {
            throw RequestException.fail("添加失败,考核记录得分添加操作人姓名不能为空!");
        }
        EvaluationProjectItemScore entity = new EvaluationProjectItemScore();
        BeanUtils.copyProperties(param, entity);
        return ResponseResult.e(ResponseCode.OK, projectItemScoreService.saveOrUpdate(entity));
    }

    @PostMapping(value = {"/update"})
    @ApiOperation(value = "修改一个考核记录得分")
    @SysLogs("修改一个考核记录得分")
    public ResponseResult update(@RequestBody ProjectItemScoreParam param){
        if (StringUtils.isEmpty(param.getId())) {
            throw RequestException.fail("修改失败,考核记录得分主键ID不能为空!");
        }
        if (StringUtils.isEmpty(param.getUpdateBy())) {
            throw RequestException.fail("修改失败,考核记录得分主键ID不能为空!");
        }
        EvaluationProjectItemScore entity = projectItemScoreService.getById(param.getId());
        BeansUtil.copyPropertiesIgnoreNull(param, entity);
        return ResponseResult.e(ResponseCode.OK, projectItemScoreService.updateById(entity));
    }

    @PostMapping(value = {"/delete"})
    @ApiOperation(value = "删除一个考核记录得分")
    @SysLogs("删除一个考核记录得分")
    public ResponseResult delete(@RequestBody ProjectItemScoreParam param){
        if (StringUtils.isEmpty(param.getId())) {
            throw RequestException.fail("修改失败,考核记录得分主键ID不能为空!");
        }
        return ResponseResult.e(ResponseCode.OK, projectItemScoreService.removeById(param.getId()));
    }
}
